/*
 * Copyright 2012-2013 Finbudget.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package finbudget.client.ui.table;

import java.util.ArrayList;

import finbudget.shared.process.events.StringPair;

public class LinkField extends Field {
	private static final long serialVersionUID = 6704137776476264844L;

	/**
	 * Stores template for the link. To get the link each item
	 * in the parameters list should be placed instead of corresponding
	 * {X} entry. Thus, for the first parameter {0} should be replaced etc. 
	 */
	private String linkTemplate;
	private ArrayList<String> parameters;
	/**
	 * Optional list of paired parameters. This list takes priority over parameters.
	 * E.a. in case pairParameters is not null it must be used but parameters should be
	 * ignored.<br/>
	 * The list contains pair of default value (left) and field name (right). In case
	 * the right part is null or empty the value should be used for substitution. Otherwise
	 * the record's value of the specified field name should be taken. In case the record's
	 * value is null or empty default value is used; otherwise record's value is used for
	 * substitution. 
	 */
	private ArrayList<StringPair> pairParameters;
	/**
	 * Optional. In case this field is not null it stores the name
	 * of the Record field that stores caption of the link cell.
	 */
	private String fieldName;
	/**
	 * Makes link just in case all the required parts found. Otherwise make inconsistent link.
	 */
	private boolean allLinkFieldsRequired;

	/** Required for serialization */
	protected LinkField() {
	}

	public LinkField(String id, String name, String linkTemplate) {
		this(name, linkTemplate, false);
		this.id = id;
	}

	public LinkField(String name, String linkTemplate) {
		this(name, linkTemplate, false);
	}

	public LinkField(String name, String linkTemplate, boolean allLinkFieldsRequired) {
		this.type = Type.LINK;
		this.captionForm = this.captionGrid = name;
		this.linkTemplate = linkTemplate;
		this.allLinkFieldsRequired = allLinkFieldsRequired;
	}

	public String getLinkTemplate() {
		return linkTemplate;
	}

	public ArrayList<String> getParameters() {
		return parameters;
	}

	public LinkField addParameter(String param) {
		if (parameters == null) {
			parameters = new ArrayList<String>(1);
		}
		parameters.add(param);
		return this;
	}

	public String getFieldName() {
		return fieldName;
	}

	public LinkField setFieldName(String fieldName) {
		this.fieldName = fieldName;
		return this;
	}

	public ArrayList<StringPair> getPairParameters() {
		return pairParameters;
	}

	public LinkField addPairParameters(StringPair pair) {
		if (pair != null) {
			// remove parameters
			parameters = null;
			if (pairParameters == null) {
				pairParameters = new ArrayList<StringPair>(1);
			}
			pairParameters.add(pair);
		}
		return this;
	}

	public LinkField addPairParameters(String left, String right) {
		return addPairParameters(new StringPair(left, right));
	}

	public boolean isAllLinkFieldsRequired() {
		return allLinkFieldsRequired;
	}

}
